﻿using System;
using System.Configuration;

namespace ZK.Common
{
    public class Settings
    {
        public static string InfraredGratingComPort => ConfigurationManager.AppSettings["InfraredGratingComPort"];

        private static DateTime GetDateTime(string key)
        {
            DateTime time;
            return DateTime.TryParse(ConfigurationManager.AppSettings[key], out time) ? time : DateTime.MinValue;
        }

        private static void SetDateTime(string key, DateTime value)
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] != null)
                config.AppSettings.Settings[key].Value = value.ToFormattedString();
            else
                config.AppSettings.Settings.Add(key, value.ToFormattedString());
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        private static bool GetBool(string key)
        {
            bool value;
            return bool.TryParse(ConfigurationManager.AppSettings[key], out value) && value;
        }

        public static int GetInt(string key)
        {
            var value = 0;
            int.TryParse(ConfigurationManager.AppSettings[key], out value);
            return value;
        }


        /// <summary>
        /// 获取webconfig节点
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string GetAppConfig(string strKey)
        {
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == strKey)
                {
                    return ConfigurationManager.AppSettings[strKey];
                }
            }
            return "";
        }

        ///<summary>
        ///在＊.exe.config文件中appSettings配置节增加一对键、值对
        ///</summary>
        ///<param name="newKey"></param>
        ///<param name="newValue"></param>
        public static void UpdateAppConfig(string newKey, string newValue)
        {
            bool isModified = false;
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == newKey)
                {
                    isModified = true;
                }
            }

            // Open App.Config of executable
            Configuration config =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // You need to remove the old settings object before you can replace it
            if (isModified)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            // Add an Application Setting.
            config.AppSettings.Settings.Add(newKey, newValue);
            // Save the changes in App.config file.
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}